﻿using System.Security.Cryptography;
using System.Text;

namespace MyWeb.Common
{
    public class StringClass
    {
        private static readonly string[] VietnameseSigns = new[]
                                                               {
                                                                   "aAeEoOuUiIdDyY",
                                                                   "áàạảãâấầậẩẫăắằặẳẵ",
                                                                   "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
                                                                   "éèẹẻẽêếềệểễ",
                                                                   "ÉÈẸẺẼÊẾỀỆỂỄ",
                                                                   "óòọỏõôốồộổỗơớờợởỡ",
                                                                   "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
                                                                   "úùụủũưứừựửữ",
                                                                   "ÚÙỤỦŨƯỨỪỰỬỮ",
                                                                   "íìịỉĩ",
                                                                   "ÍÌỊỈĨ",
                                                                   "đ",
                                                                   "Đ",
                                                                   "ýỳỵỷỹ",
                                                                   "ÝỲỴỶỸ"
                                                               };

        public static string Encode(string method, string input)
        {
            byte[] data;
            switch (method)
            {
                case "sha1":
                    data = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                case "sha256":
                    data = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                case "sha384":
                    data = SHA384.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                case "sha512":
                    data = SHA512.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                default:
                    data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
            }
            var sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        public static bool Kiemtrakitu(char ch)
        {
            char[] a = {'@', '!', '`', '~', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '_', '='};
            foreach (char c in a)
            {
                if (c.Equals(ch))
                    return true;
            }
            return false;
        }

        public static string LocDau(string str)
        {
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)

                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
            }

            return str;
        }

        public static string XuLiChuoi(string str)
        {
            while (str.IndexOf(" ") >= 0) 
                str = str.Replace(" ", "");      
            return str;
        }

        public static string CatKiTu(string str)
        {
            str = str.Trim();
            str = LocDau(str);
            string[] a = str.Split(' ');
            string result = "";
            foreach (string word in a)
            {
                result += word[0].ToString();
            }
            return result;
        }
    }
}